/*
 * Copyright (C) 2013 Alex Kuiper <http://www.nightwhistler.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.nightwhistler.htmlspanner.handlers;

import net.nightwhistler.htmlspanner.FontFamily;
import net.nightwhistler.htmlspanner.SpanStack;

import net.nightwhistler.htmlspanner.css.CSSCompiler;
import net.nightwhistler.htmlspanner.style.Style;
import org.htmlcleaner.TagNode;

import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.xlh.Utils.FUti;
import com.xlh.interf.IActivity;
import com.xlh.outside.MainActivity;

/**
 * Handler for font-tags
 */
public class FontHandler extends StyledTextHandler {

    public FontHandler(IActivity atv) {
        super(new Style());
        this.setAtv(atv);

    }

    public IActivity getAtv() {
        return atv;
    }

    public void setAtv(IActivity atv) {
        this.atv = atv;
    }

    private IActivity atv;


    private String[] styleList = new String[]{"color","size","cmds","background"};
	@Override
	public void handleTagNode(TagNode node, SpannableStringBuilder builder,
			int start, int end, Style style, SpanStack spanStack) {

        if ( getSpanner().isAllowStyling() ) {
            String tAry[],tSary[],tStr,cmds="",background="";
            String face = node.getAttributeByName("face");
            String size = node.getAttributeByName("size");
            String color = node.getAttributeByName("color");
            //直接的STYLE属性
            String styles = node.getAttributeByName("style");




            //分析style
            if(styles.indexOf(";")>-1){
                //如果有;表示多个属性
                tAry = styles.split(";");
                for(String s : tAry){
                    tSary = s.split(":");
                    tStr = tSary[0];

                    if( tStr.equals("color") && !tSary[1].isEmpty() && !tSary[1].equals("undefined") ){
                        color = tSary[1];
                    }else if(tStr.equals("size") && !tSary[1].isEmpty() && !tSary[1].equals("undefined")){
                        size = tSary[1];
                    }else if(tStr.equals("cmds") && !tSary[1].isEmpty() && !tSary[1].equals("undefined")){
                        cmds = tSary[1];
                    }else if(tStr.equals("background") && !tSary[1].isEmpty() && !tSary[1].equals("undefined")){
                        background = tSary[1];
                    }else{
                        break;
                    }
                }


            }else{
                tAry = styles.split(":");
                // ArrayUtils.indexOf(styleList, tAry[0] );
                tStr = tAry[0];

                if( tStr.equals("color") && !tAry[1].isEmpty() && !tAry[1].equals("undefined")){
                    color = tAry[1];
                }else if(tStr.equals("size") && !tAry[1].isEmpty() && !tAry[1].equals("undefined")){
                    size = tAry[1];
                }else if(tStr.equals("cmds") && !tAry[1].isEmpty() && !tAry[1].equals("undefined")){
                    cmds = tAry[1];
                }else if(tStr.equals("background") &&  !tAry[1].isEmpty() && !tAry[1].equals("undefined")){
                    background = tAry[1];
                }else{
                    return;
                }

            }



            FontFamily family = getSpanner().getFont(face);

            style = style.setFontFamily(family);

            if ( size != null ) {
//                CSSCompiler.StyleUpdater updater = CSSCompiler.getStyleUpdater("font-size", size);
//
//                if ( updater != null ) {
//                    style = updater.updateStyle(style, getSpanner() );
//                }

                spanStack.pushSpan( new AbsoluteSizeSpan(FUti.dip2px(this.getAtv().getActivity(),Integer.valueOf(size))),start,end);



            }

            if ( color != null && getSpanner().isUseColoursFromStyle() ) {

                CSSCompiler.StyleUpdater updater = CSSCompiler.getStyleUpdater("color", color);

                if ( updater != null ) {
                    style = updater.updateStyle(style, getSpanner() );
                }
            }


            if ( background != null && !background.isEmpty() && getSpanner().isUseColoursFromStyle() ) {

                CSSCompiler.StyleUpdater updater = CSSCompiler.getStyleUpdater("background-color", color);

                if ( updater != null ) {
                    style = updater.updateStyle(style, getSpanner() );
                }
            }


            if ( cmds != null && !cmds.isEmpty() ) {

                spanStack.pushSpan(new onClickSpan(cmds), start, end);
            }





        }

        super.handleTagNode(node, builder, start, end, style, spanStack);
	}

    private class onClickSpan extends ClickableSpan implements View.OnClickListener {



        private String cmds;

        public String getCmds() {
            return cmds;
        }

        public void setCmds(String cmds) {
            this.cmds = cmds;
        }

        public onClickSpan(String cmd){
            super();
            this.setCmds(cmd);
        }

        @Override
        public void onClick(View widget) {
            // TODO Auto-generated method stub

            //具体代码，可以是跳转页面，可以是弹出对话框，下
            // mContext.startActivity(new Intent(mContext,MainActiv面是跳转页面ity.class));
            if (this.cmds.isEmpty()){
                Toast tst = Toast.makeText(widget.getContext(), "点击标签指令为空！", Toast.LENGTH_SHORT);
                tst.setGravity(Gravity.TOP, 0, 100);
                tst.show();
                return;
            }

            ((MainActivity)widget.getContext()).getmSocketClient().sendMsg(cmds);
            ((MainActivity)widget.getContext()).closeAlertBox();

        }
    }
	
}
